import * as echarts from 'echarts/core'
import {
  PieChart,
  BarChart,
  RadarChart,
  // 系列类型的定义后缀都为 SeriesOption
  PieSeriesOption,
  BarSeriesOption,
  RadarSeriesOption,
  LineChart,
  LineSeriesOption,
} from 'echarts/charts'
import {
  TitleComponent,
  // 组件类型的定义后缀都为 ComponentOption
  TitleComponentOption,
  GridComponent,
  GridComponentOption,
  TooltipComponent,
  TooltipComponentOption,
  // 数据集组件
  DatasetComponent,
  DatasetComponentOption,
  // 内置数据转换器组件 (filter, sort)
  TransformComponent,
} from 'echarts/components'
import { LabelLayout, UniversalTransition } from 'echarts/features'
import { CanvasRenderer } from 'echarts/renderers'

// 通过 ComposeOption 来组合出一个只有必须组件和图表的 Option 类型
export type ECOption = echarts.ComposeOption<
  PieSeriesOption
  | BarSeriesOption
  | RadarSeriesOption
  | TooltipComponentOption
  | LineSeriesOption
  | TitleComponentOption
  | GridComponentOption
  | DatasetComponentOption
>

// 注册必须的组件
echarts.use([
  TitleComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent,
  TooltipComponent,
  BarChart,
  PieChart,
  LineChart,
  RadarChart,
  LabelLayout,
  UniversalTransition,
  CanvasRenderer,
])

const chart = (id: string): echarts.ECharts | null => {
  const dom = document.getElementById(id);
  if (dom) {
    return echarts.init(dom)
  } else {
    return null
  }
}
export default chart
